package com.cb.admin.controllers;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.cb.admin.service.IConfigInfoService;
import com.cb.util.DateUtils;
import com.cb.util.RsaUtil;

@Controller
@RequestMapping("/sk")
public class SecurityKeyController extends BaseController {
	
	private static Logger logger = Logger.getLogger(SecurityKeyController.class.getName());
	
	@Autowired
	private IConfigInfoService configInfoService;
	
	@RequestMapping("show")
	public String show(Model model){
		return "key/key";
	}

	@RequestMapping(path = "/saveKey", method = RequestMethod.POST)
	public String saveKey(Model model,@RequestParam("key") String key){
		if(key==null || "".equals(key)){
			return "redirect:../logout";
		}
		
		String l_key = configInfoService.querySecurityKey();
		
		if(l_key!=null && !"".equals(l_key)){
			//存在Key
			configInfoService.updateSecurityKey(key);
		}else{
			//不存在Key
			model.addAttribute("msg","不存在Key,验证失败!");
			return "key/key";
		}
		
		return "redirect:../logout";
	}
	
	@RequestMapping("delayKey")
	public String delayKey(Model model) throws Exception{
		String l_key = configInfoService.querySecurityKey();
		
		//服务器 当前时间
		String content = DateUtils.getMinusTime(DateUtils.DATE_SMALL_STR, -30);
		String encrypt = RsaUtil.encryptByPublicKey(content);
		
		if(l_key!=null && !"".equals(l_key)){
			//存在Key
			configInfoService.updateSecurityKey(encrypt);
		}else{
			//不存在Key
			model.addAttribute("msg","不存在Key,验证失败!");
			return "key/key";
		}
		
		return "redirect:../logout";
	}
}